package com.suray.wcs.web.system.controller;

import com.suray.basic.wcs.plc.utils.NetAddress;
import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.enums.DeviceType;
import com.suray.wcs.service.enums.NetAddressType;
import com.suray.wcs.service.pojo.BaseEum;
import com.suray.wcs.service.pojo.NetDevice;
import com.suray.wcs.service.system.entity.NetAddressManageDB;
import com.suray.wcs.service.system.service.impl.MachineTaskServiceImpl;
import com.suray.wcs.service.system.service.impl.NetAddressManageDBServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @param
 * @author cjh
 * @since 2022年11月17日
 */
@RestController
@RequestMapping("/netdevice")
public class NetDeviceDBController extends BaseController {

    @Autowired
    private NetAddressManageDBServiceImpl devicedbServiceImpl;
    @Autowired
    private MachineTaskServiceImpl machineTaskService;

    private Logger log = LoggerFactory.getLogger(this.getClass());


    /**
     * 查找接口
     *
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('netdevice:list')")
    @GetMapping(value = "/list")
    @ResponseBody
    @ApiOperation("按条件查找设备")
    public TableDataInfo getTaskPage(NetDevice device) {
        NetAddressManageDB devicedb = TransformDb(device);
        startPage();
        List<NetAddressManageDB> deviceList = devicedbServiceImpl.findDeviceList(devicedb,devicedb.getType());
        for(NetAddressManageDB db:deviceList){
            if(db.getType()!=null){
                db.setNetType(db.getType().getValue());
            }
        }
        return getDataTable(deviceList);
    }

    /**
     * 新增设备
     *
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('netdevice:add')")
    @Log(title = "设备管理", method = "新增设备", businessType = BusinessType.INSERT)
    @PostMapping(value = "/insertDevice")
    @ResponseBody
    @ApiOperation("新增设备")
    public ResponseData insertDevice(@RequestBody NetDevice device) {
        NetAddressManageDB devicedb = TransformDb(device);
        if (StringUtils.isEmpty(device.getIp())) {
            return ResponseData.error("设备IP不存在");
        }
        if (device.getPort() == null) {
            return ResponseData.error("设备端口不存在");
        }
        if (device.getNetType() == null) {
            return ResponseData.error("设备类型不存在");
        }
        devicedb.setType(NetAddressType.parsePlcDeviceType(device.getNetType()));
        devicedbServiceImpl.insertDevice(devicedb);
        return ResponseData.success("插入成功");
    }



    /**
     * 更新设备
     *
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('netdevice:edit')")
    @Log(title = "设备管理", method = "更新设备", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateDevice")
    @ResponseBody
    @ApiOperation("更新设备")
    public ResponseData updateDevice(@RequestBody NetDevice device) {
        NetAddressManageDB devicedb = TransformDb(device);
        if (device.getId() == null) {
            return ResponseData.error("设备ID不存在");
        } else if (device.getIp().isEmpty()) {
            return ResponseData.error("设备IP不存在");
        } else if (device.getPort() == null) {
            return ResponseData.error("设备端口不存在");
        } else if (device.getNetType() == null) {
            return ResponseData.error("设备类型不存在");
        } else {
            devicedbServiceImpl.updateDevice(devicedb);
            return ResponseData.success("更新成功");
        }

    }

    @PreAuthorize("@ss.hasPermi('netdevice:query')")
    @RequestMapping(value = "/queryDeviceType", method = RequestMethod.GET)
    @ApiOperation("查询设备类型")
    public ResponseData queryDeviceType() {
        List<DeviceType> deviceManagerType = (Arrays.asList(DeviceType.values()));
        List<BaseEum> deviceTypeList = new ArrayList<BaseEum>();
        for (DeviceType deviceType : deviceManagerType) {
            deviceTypeList.add(new BaseEum(deviceType.getValue(), deviceType.toString()));
        }
        return ResponseData.success("查询完成", deviceTypeList);
    }
    /**
     * 前后端交互对象转换成DB对象
     *
     * @param device
     * @return
     */
    private NetAddressManageDB TransformDb(NetDevice device) {
        NetAddressManageDB devicedb = new NetAddressManageDB();
        BeanUtils.copyProperties(device, devicedb);
        devicedb.setType(device.getType() == null ? null : NetAddressType.parsePlcDeviceType(device.getType()));
        return devicedb;
    }
}
